package com.iflysse.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.iflysse.config.ConfigParam;
import com.iflysse.consts.RoleEnum;
import com.iflysse.dao.AccountDao;
import com.iflysse.dao.StudentDao;
import com.iflysse.dao.TeacherDao;
import com.iflysse.entity.Account;
import com.iflysse.entity.Student;
import com.iflysse.entity.Teacher;
import com.iflysse.service.AccountService;

import cn.hutool.core.map.MapUtil;

import com.iflysse.dto.UserLoginDto;

@Transactional
@Service
public class AccountServiceImpl implements AccountService {

	private final static Log log = LogFactory.getLog(AccountServiceImpl.class);

    @Autowired
    private AccountDao userDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ConfigParam configParam;
    @Override
    public Account login(UserLoginDto form) {
        Account account = userDao.findUserByAccountInfoAndPassword(form);
        if(account != null) {
            //更新登录时间
            userDao.updateLoginDate(account.getAccountId());
        }
        return account;
    }

    @Override
    public String queryIdByAccountIdAndRole(String accountId, String role) {
        if(role.equals(RoleEnum.ROLETEACHER.getRoleId())){
            //查询老师
            Teacher t = teacherDao.getTeacherByAccountId(accountId);
            log.debug("查询到的老师信息：" + t);
            if(t != null){
                return t.getTeacherId();
            }
        }else if(role.equals(RoleEnum.ROLESTUDENT.getRoleId())){
            //查询学生
            Student s = studentDao.getStudentByAccountId(accountId);
            log.debug("查询到的学生信息：" + s);
            if(s != null){
                return s.getStudentId();
            }
        }
        return null;
    }
    
    @Override
	public Map findUserInfoByUserIdAndRole(String userId, String role) {
		if(role.equals(RoleEnum.ROLETEACHER.getRoleId())){
            //查询老师
            Teacher t = teacherDao.getTeacherByTeacherId(userId);
            log.debug("查询到的老师信息：" + t);
            if(t != null){
                return MapUtil.of(new Object[][] {
                	{"baseImg", configParam.getStaticResourcePrefix() + t.getHeadImage()},
                	{"name", t.getName()},
                	{"email", t.getEmail()},
                	{"gender", t.getGender()},
                	{"nickName", t.getNickName()},
                	{"hobby", new ArrayList<>()},
                	{"personalSignature", t.getPersonalSignature()},
                	{"teacherId", t.getTeacherId()}
                });
            }
        }else if(role.equals(RoleEnum.ROLESTUDENT.getRoleId())){
            //查询学生
            Student s = studentDao.getStudentByStudentId(userId);
            log.debug("查询到的学生信息：" + s);
            if(s != null){
            	Map map = MapUtil.of(new Object[][] {
                	{"baseImg", configParam.getStaticResourcePrefix() + s.getHeadImage()},
                	{"name", s.getStudentName()},
                	{"email", s.getEmail()},
                	{"gender", s.getGender()},
                	{"nickName", s.getNickName()},
                	{"hobby", new ArrayList<>()},
                	{"personalSignature", s.getPersonalSignature()},
                	{"teacherId", null}
                });
            	
				if (s.getHobbies() != null && !"".equals(s.getHobbies())){
					map.put("hobby", Arrays.asList(s.getHobbies().split(",")));
				}
            	return map;
            }
        }
		
		return null;
	}



}
